var api = require("../../api/api.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
var imgUp = require("../../template/imgUp/imgUp.js");
var storageManage = require("../../manage/storageManage.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "paymentState": 0,
    "commodity_list": [],
    "complate_time": "",
    "create_time": "",
    "maxLength": 3,
    "balance": 0,
    "imglist": [],
    "isAddBtnShow": true,
    "on_credit_reason": null,
    "order_no": "",
    "order_status": "",
    "order_status_name": "",
    "pay_time": "",
    "pay_type": null,
    "phone": "",
    "recv_addr": "",
    "recv_name": "",
    "remark": "",
    "store_info": {},
    "time": "",
    "total_price": '',
    "balance": 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    imgUp.init.apply(this, []);//引入方法
    this.setData({
      // order_no:"order4965948332"
      order_no: options.order_id
    })
    this.getData()

  },
  onReady: function () {
    //获得modalContainer组件
    this.modalContainer = this.selectComponent("#modalPay");
  },
  //结算生产订单且弹出选择支付方式
  showPay: utils.throttle( function (e) {
    var that = this;
    api.default.send_template(e);
    var payload = {
      order_no: that.data.order_no,
      balance: that.data.balance,
      commodity_cost: that.data.total_price
    }
    this.modalContainer.showModalContainer(payload);
  }),


  _pay: function (e) {
    //支付成功返回支付方式的状态值
    var paymentState = e.detail.paymentState;
    var order_status='';
    if (paymentState == 0) {
     order_status="pending_send"

    } else if (paymentState == 3) {
      order_status="pending_send"

    } else {
      order_status="pending_check"
    }
    event.notify("paySuccess", { order_status: order_status});
    wx.navigateBack()
  },
  again: utils.throttle(function(){
    var that=this;
    var payload={
      "order_id": that.data.order_no
    }
    if ( that.data.pay_type==1 ){
      payload.detail = that.data.imglist;
    } else if ( that.data.pay_type == 2){
      payload.detail = that.data.on_credit_reason
    }
    api.default.api('order_reset', payload, function (data) {

      event.notify("paySuccess", { order_status: "pending_check" });
      wx.navigateBack()

    }, function () { });
  }),
  getData: function () {
    var that = this;
    api.default.api('order_detail', {
      "order_id": that.data.order_no
    }, function (data) {
      that.setData({
        "commodity_list": data.data.commodity_list,
        "complate_time": data.data.complate_time,
        "create_time": data.data.create_time,
        "imglist": data.data.images,
        "on_credit_reason": data.data.on_credit_reason,
        "order_no": data.data.order_shot_id,
        "order_status": data.data.order_status,
        "order_status_name": data.data.order_status_name,
        "pay_time": data.data.pay_time,
        "pay_type": data.data.pay_type,
        "phone": data.data.phone,
        "recv_addr": data.data.recv_addr,
        "recv_name": data.data.recv_name,
        "remark": data.data.remark,
        "store_info": data.data.store_info,
        "time": data.data.time,
        "total_price": data.data.total_price


      })
    }, function () { });
  },
 
  toLogistics: utils.throttle(function () {
    wx.navigateTo({
      url: '../logistics/logistics',
    })
  }),
  //赊货备注
  creaditChange: function (e) {
    this.setData({
      "on_credit_reason": e.detail.value
    });
  }
})